package com.xltq.controller;

import com.xltq.entity.Orders;
import com.xltq.service.IOrdersService;
import com.xltq.service.impl.IOrdersServiceImpl;
import com.xltq.utils.Constants;
import com.xltq.utils.ZxingUtils;
import com.xltq.wxpay.common.wx.WxConstants;
import com.xltq.wxpay.servcie.WxMenuService;
import com.xltq.wxpay.servcie.impl.WxMenuServiceImpl;

import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.ByteArrayOutputStream;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Objects;

//微信支付Controller
@WebServlet("/wxPay")
public class PayController extends  BaseServlet {
     //提供一个微信订单需要的md5值
    private final  String singType = WxConstants.SING_MD5;
    //构建微信支付订单
    public String wxpreOrder(HttpServletRequest request, HttpServletResponse response) throws Exception{
        //1.获取微信下单页面的必要参数
        String orderId = request.getParameter("orderId");
        String price = request.getParameter("price");
        //2.调用订单Service处理业务操作
        IOrdersService ordersService = new IOrdersServiceImpl();
        Orders orders = ordersService.findOrdersByOid(orderId);
        //这里以页面上传递金额为准进行微信接口调用 --> 位置支付比例是1:100  1元 相当于是 0.01元
        orders.setOcount(new BigDecimal(price));

        //2.调用微信服务器创建微信订单
        //2.1 获取二维码链接
        WxMenuService wxMenuService = new WxMenuServiceImpl();
        String codeUrl = wxMenuService.wxPayUrl(orders, singType);
        //3.获取微信返回的支付url ,将url放置到session 以map的形式方法, key订单编号 value是url
        HttpSession session = request.getSession();
        HashMap<String,String> map = new HashMap<>();
        map.put(orders.getOid(), codeUrl);
        session.setAttribute("wxUrlMap", map);
        //4.跳转到支付页面,页面带有支付而二维码
        request.setAttribute("oid", orderId);
        //wxpay.jsp 支付页面
        return Constants.FORWARD+"/wxpay.jsp";

    }
    //支付页面传递订单编号,获取支付二维码
    public void getQrCode(HttpServletRequest request, HttpServletResponse response) throws Exception{
                //1.获取订单编号
        String oid = request.getParameter("oid");
        //2.从session中获取订单编号对应url
        HashMap<String,String> wxUrlMap = (HashMap<String,String>)request.getSession().getAttribute("wxUrlMap");
        String wxurl = wxUrlMap.get(oid);
        //3.生成二维码
        ByteArrayOutputStream qrCode = ZxingUtils.createQrCode(wxurl);
        //设置响应头
        response.setHeader("Cache-Control", "no-cache");
        response.setContentType("image/png");
        //在通过影响将二进制写入到页面
        ServletOutputStream outputStream = response.getOutputStream();
        outputStream.write(qrCode.toByteArray());
        outputStream.flush();
        outputStream.close();

    }
    //查询微信订单状态
    public String wxQueryOrder(HttpServletRequest requestr,HttpServletResponse response)throws Exception{
         //获取订单号信息
        String oid = requestr.getParameter("oid");
        WxMenuService wxMenuService = new WxMenuServiceImpl();
        //result就是支付之后效果值
        String result = wxMenuService.wxOrderQuery(oid, singType);
        if(Objects.nonNull(result) && result.equalsIgnoreCase("SUCCESS")){
            return  "1" ;
        }else{
            return  result;
        }
    }
}
